package com.hxd.otc.module.home.adapter;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.media.MediaPlayer;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.hxd.otc.R;
import com.hxd.otc.base.BaseAdapter;
import com.hyphenate.helpdesk.domain.CommentEntity;
import com.hyphenate.helpdesk.domain.TicketEntity;
import com.hyphenate.helpdesk.easeui.recorder.MediaManager;
import com.hyphenate.helpdesk.util.ISO8601DateFormat;
import com.hyphenate.util.DateUtils;

import java.text.ParseException;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by lzy on 2019/1/16 0016.
 * description ${留言详情Adapter}
 */
public class TicketDetailAdapter extends BaseAdapter<RecyclerView.ViewHolder, CommentEntity> {
    private static final int VIEW_TYPE_HEADER = 1;
    public static final int VIEW_TYPE_NOR = 0;
    private TicketEntity mTicketEntity;
    ISO8601DateFormat mDateFormat = new ISO8601DateFormat();

    public TicketDetailAdapter(Context context) {
        super(context);
    }

    @Override
    protected void onBindHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case VIEW_TYPE_HEADER:
                onBindHeaderHolder((HeaderHolder) holder, position);
                break;
            default:
                onBindNorHolder((CommentHolder) holder, getItem(position), position);
                break;
        }
    }

    private void onBindHeaderHolder(HeaderHolder holder, int position) {
        if (mTicketEntity == null) {
            holder.ticketContent.setText("");
            return;
        }

        TicketEntity.CreatorBean creator = mTicketEntity.getCreator();
        if (creator == null) return;

        holder.tvTicketName.setText("null".equals(creator.getName()) ? "" : creator.getName());
        holder.tvTicketPhone.setText(creator.getPhone());
        holder.tvTicketEmail.setText(creator.getEmail());
        holder.tvTicketTheme.setText(mTicketEntity.getSubject());
        holder.tvTicketDetail.setText(mTicketEntity.getContent());
        try {
            holder.tvTicketDate.setText(DateUtils.getTimestampString(mDateFormat.parse(mTicketEntity.getUpdated_at())));
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    private void onBindNorHolder(CommentHolder holder, CommentEntity item, int position) {
        holder.tvName.setText(item.getCreator().getName());
        try {
            holder.tvTimestamp.setText(DateUtils.getTimestampString(mDateFormat.parse(item.getUpdated_at())));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        holder.tvContent.setText(item.getContent());
//        final List<CommentEntity.AttachmentsBean> attachments = item.getAttachments();
//        if (attachments != null && attachments.size() > 0) {
//            holder.flowLayout.setVisibility(View.VISIBLE);
//            holder.flowLayout.removeAllViews();
//            for (final CommentEntity.AttachmentsBean bean : attachments) {
//                final String remoteUrl = bean.getUrl();
//                final String localName = EncryptUtils.getMd5(bean.getUrl()) + "-" + bean.getName();
//                final String localPath = PathUtil.getInstance().getFilePath() + File.separator + localName;
//                final String fileType = bean.getType();
//                if (fileType != null && fileType.equals("audio")) {
//                    View audioView = mInflater.inflate(R.layout.em_comment_audio_view, null);
//                    audioView.setOnClickListener(new View.OnClickListener() {
//                        @Override
//                        public void onClick(final View v) {
//                            File file = new File(localPath);
//                            if (file.exists()) {
//                                playVoiceItem(v, localPath);
//                            } else {
//                                mOnAttachmentsListener.downloadFile(remoteUrl, localName, fileType);
//                            }
//                        }
//                    });
//                    FlowLayout.LayoutParams lp = new FlowLayout.LayoutParams(DensityUtil.dip2px(mContext, 50), DensityUtil.dip2px(mContext, 30));
//                    lp.topMargin = DensityUtil.dip2px(mContext, 5);
//                    lp.bottomMargin = DensityUtil.dip2px(mContext, 5);
//                    lp.leftMargin = DensityUtil.dip2px(mContext, 5);
//                    lp.rightMargin = DensityUtil.dip2px(mContext, 5);
//                    holder.flowLayout.addView(audioView, lp);
//
//                } else {
//                    TextView textView = (TextView) mInflater.inflate(R.layout.em_comment_file_textview, null);
//                    textView.setText(bean.getName());
//                    textView.setOnClickListener(new View.OnClickListener() {
//                        @Override
//                        public void onClick(View v) {
//                            if (mOnAttachmentsListener == null) return;
//
//                            File file = new File(localPath);
//                            if (file.exists()) {
//                                mOnAttachmentsListener.openLocalFile(file);
//                            } else {
//                                mOnAttachmentsListener.downloadFile(remoteUrl, localName, fileType);
//                            }
//
//                        }
//                    });
//                    FlowLayout.LayoutParams lp = new FlowLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, DensityUtil.dip2px(mContext, 30));
//                    lp.topMargin = DensityUtil.dip2px(mContext, 5);
//                    lp.bottomMargin = DensityUtil.dip2px(mContext, 5);
//                    holder.flowLayout.addView(textView, lp);
//                }
//            }
//
//        } else {
//            holder.flowLayout.setVisibility(View.GONE);
//        }
    }

    private View animView;

    public void playVoiceItem(View v, String voiceLocalPath) {
        //播放动画
        if (animView != null) {
            animView.setBackgroundResource(R.drawable.hd_chatfrom_voice_playing);
            animView = null;
        }

        animView = v.findViewById(R.id.id_recorder_anim);
        animView.setBackgroundResource(R.drawable.hd_voice_from_icon);
        AnimationDrawable anim = (AnimationDrawable) animView.getBackground();
        anim.start();

        //播放音频
        MediaManager.playSound(mContext, voiceLocalPath, new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                animView.setBackgroundResource(R.drawable.hd_chatfrom_voice_playing);
            }
        });
    }

//    private OnAttachmentsListener mOnAttachmentsListener;
//
//    public void setOnAttachmentsListener(OnAttachmentsListener onAttachmentsListener) {
//        mOnAttachmentsListener = onAttachmentsListener;
//    }
//
//    interface OnAttachmentsListener {
//        void openLocalFile(File file);
//
//        void downloadFile(String remoteUrl, String localName, String fileType);
//    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        switch (viewType) {
            case VIEW_TYPE_HEADER:
                return new HeaderHolder(mInflater.inflate(R.layout.item_ticket_detail_header, parent, false));
            default:
                return new CommentHolder(mInflater.inflate(R.layout.item_ticket_detail_comment, parent, false));
        }
    }

    public void setHeaderData(TicketEntity ticketEntity) {
        mTicketEntity = ticketEntity;
    }

    @Override
    protected int getHeaderLayoutCount() {
        return 1;
    }

    @Override
    public int getItemViewType(int position) {
        return position == 0 ? VIEW_TYPE_HEADER : VIEW_TYPE_NOR;
    }

    static class HeaderHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_ticket_name)
        TextView tvTicketName;
        @BindView(R.id.tv_ticket_phone)
        TextView tvTicketPhone;
        @BindView(R.id.tv_ticket_email)
        TextView tvTicketEmail;
        @BindView(R.id.tv_ticket_theme)
        TextView tvTicketTheme;
        @BindView(R.id.tv_ticket_detail)
        TextView tvTicketDetail;
        @BindView(R.id.tv_ticket_date)
        TextView tvTicketDate;
        @BindView(R.id.ticketContent)
        TextView ticketContent;

        HeaderHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    static class CommentHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.avatar)
        ImageView ivAvatar;
        @BindView(R.id.name)
        TextView tvName;
        @BindView(R.id.timestamp)
        TextView tvTimestamp;
        @BindView(R.id.content)
        TextView tvContent;

        CommentHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
